package com.financing.platform.web.api;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.dto.SalesPrizesPeopleDTO;
import com.financing.platform.service.ISalesPrizesPeopleService;
import com.financing.platform.util.DTOdateSet;

@Controller("salesPrizesPeopleController")
@RequestMapping("/api/salesPrizesPeople")
public class SalesPrizesPeopleController extends BaseAction  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private ISalesPrizesPeopleService salesPrizesPeopleService;
	
	@RequestMapping("/listSalesPrizesPeople.shtml")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, SalesPrizesPeopleDTO salesPrizesPeople,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query salesPrizesPeople list begin .......");
			
			salesPrizesPeople.setBegin(pager.getStartIndex());
			salesPrizesPeople.setPageSize(pager.getPageSize());
			salesPrizesPeople.setDeleteTag("1");
			int count = salesPrizesPeopleService.queryListCount(salesPrizesPeople);
			
			pager.setTotalCount(count);
			
			List<SalesPrizesPeopleDTO> salesPrizesPeopleList = null;
			
			if(count > 0){
				salesPrizesPeopleList = salesPrizesPeopleService.queryList(salesPrizesPeople);
			}else{
				salesPrizesPeopleList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", salesPrizesPeopleList);
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query salesPrizesPeople list end .......");
		}catch(Exception e){
			log.error("query salesPrizesPeople error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/addSalesPrizesPeople.shtml")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, SalesPrizesPeopleDTO salesPrizesPeople){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add salesPrizesPeople  begin .......");
			
			DTOdateSet.setCreateInfo(salesPrizesPeople,request.getSession());
			DTOdateSet.setUpdateInfo(salesPrizesPeople,request.getSession());
			
			int id = salesPrizesPeopleService.insert(salesPrizesPeople);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add salesPrizesPeople list end .......");
		}catch(Exception e){
			log.error("add salesPrizesPeople error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	
	@RequestMapping("/addBatchSalesPrizesPeople.shtml")
	@ResponseBody
	public ResultMap addBatchSalesPrizes(HttpServletRequest request){
		ResultMap result = new ResultMap();
		
		try{
			logger.info(" addBatchSalesPrizes  begin .......");
			
			String sales=request.getParameter("salesPrizesPeople");
			
			if(StringUtils.isNotBlank(sales)){
				String[] salesPrizes = sales.split("\n");
				//判断名单中是否有重复
				Set<String> old=new HashSet<String>();
				for (int i = 0; i < salesPrizes.length; i++) {
					String phone = salesPrizes[i].trim();
					String o = phone.split(",")[0];
					if(old.contains(o)){
						result.setAttribute("phone",o);
						result.setStatusCode(ResultCode.STATUS_CODE_4021);
						result.setStatus(ResultMap.STATUS.ERROR);
						return result;
					}else{
						old.add(o);
					}
				}
				
				//修改之前所有的记录为2 删除
				if(salesPrizes.length>0){
					SalesPrizesPeopleDTO s=new SalesPrizesPeopleDTO();
					s.setDeleteTag("2");
					salesPrizesPeopleService.update(s);
				}
				for (int i = 0; i < salesPrizes.length; i++) {
					String saleDto = salesPrizes[i];
					if(StringUtils.isNotBlank(saleDto)){
						SalesPrizesPeopleDTO dto=new SalesPrizesPeopleDTO();
						String[] d = saleDto.split(",");
						if(d.length<2||!d[0].trim().matches("\\d{11}")||!d[1].trim().matches("\\d+")){
							continue;
						}
						dto.setSalesPhone(d[0]);
						dto.setPrizesCount(Integer.parseInt(d[1].trim()));
						if(d.length>2){
							dto.setSalesName(d[2].trim());
						}
						if(d.length>3&&d[3].trim().matches("\\d+")){
							dto.setSalesAmount(Integer.parseInt(d[3].trim()));
						}
						DTOdateSet.setCreateInfo(dto,request.getSession());
						DTOdateSet.setUpdateInfo(dto,request.getSession());
						dto.setDeleteTag("1");
						salesPrizesPeopleService.insert(dto);
					}
				}
			}
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			logger.info("addBatchSalesPrizes list end .......");
		}catch(Exception e){
			log.error("addBatchSalesPrizes error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailSalesPrizesPeople.shtml")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, SalesPrizesPeopleDTO salesPrizesPeople){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail salesPrizesPeople  begin .......");
			
			Integer id = salesPrizesPeople.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			SalesPrizesPeopleDTO dto = salesPrizesPeopleService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail salesPrizesPeople  end .......");
		}catch(Exception e){
			log.error("query detail salesPrizesPeople error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteSalesPrizesPeople.shtml")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, SalesPrizesPeopleDTO salesPrizesPeople){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete salesPrizesPeople  begin .......");
			
			Integer id = salesPrizesPeople.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			salesPrizesPeopleService.delete(id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete salesPrizesPeople  end .......");
		}catch(Exception e){
			log.error("delete salesPrizesPeople error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateSalesPrizesPeople.shtml")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, SalesPrizesPeopleDTO salesPrizesPeople){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update salesPrizesPeople  begin .......");
			
			Integer id = salesPrizesPeople.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSet.setUpdateInfo(salesPrizesPeople,request.getSession());
			
			salesPrizesPeopleService.update(salesPrizesPeople);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update salesPrizesPeople list end .......");
		}catch(Exception e){
			log.error("update salesPrizesPeople error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceSalesPrizesPeople.shtml")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, SalesPrizesPeopleDTO salesPrizesPeople){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce salesPrizesPeople  begin .......");
			
			Integer id = salesPrizesPeople.getId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			DTOdateSet.setUpdateInfo(salesPrizesPeople,request.getSession());
			
			salesPrizesPeopleService.updateForce(salesPrizesPeople);
			
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce salesPrizesPeople list end .......");
		}catch(Exception e){
			log.error("updateForce salesPrizesPeople error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
}
